package alog;

public class sort2 {
    public static void main(String[] args) {
        int[] nums = new int[]{1,3,0,-1,7,6};
//        quick(nums, 0, nums.length - 1);
//        bubble(nums);
        selectSort(nums);
        for(int i : nums)
            System.out.println(i + " ");
    }
    public static void quick(int[] num, int left, int right){
        /**
         * 细节判断
         */
        if(right <=  left)
            return;
        int i = left, j = right;
        while(i < j){
            while (i < j && num[j] >= num[left])
                j--;
            while(i < j && num[i] <= num[left])
                i++;
            if(i < j){
                int p = num[i];
                num[i] = num[j];
                num[j] = p;
            }
        }
        int t = num[i];
        num[i] = num[left];
        num[left] = t;
        quick(num, left, i - 1);
        quick(num, i + 1, right);
    }

    public static void bubble(int[] num){
        /**
         * i， j别写错
         */
        int f = 0;
        for (int i = 0; i < num.length - 1; i++) {
            f = 1;
            for (int j = 0; j < num.length - 1 - i; j++) {
                if(num[j] > num[j + 1]){
                    int temp = num[j];
                    num[j] = num[j + 1];
                    num[j + 1] = temp;
                    f = 0;
                }
            }
            if(f == 1)
                break;
        }
    }

    public static void selectSort(int[] num){
        int t;
        int index;
        for (int i = 0; i < num.length - 1; i++) {
            t = num[i];
            index = i;
            for (int j = i + 1; j < num.length; j++) {
                if(num[j] < t){
                    t = num[j];
                    index = j;
                }
            }
            /**
             * 注意最后的交换，还有前面的i，j等
             */
            num[index] = num[i];
            num[i] = t;
        }
    }
}
